package com.newway.back.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newway.back.entity.SysUserEntity;
import com.newway.back.entity.approval.ApprovalNode;
import com.newway.back.entity.approval.ApprovalTask;
import com.newway.back.entity.approval.ApprovalTemplate;
import com.newway.back.enums.ApproverTypeEnum;
import com.newway.back.mapper.SysUserMapper;
import com.newway.back.mapper.approval.ApprovalInstanceMapper;
import com.newway.back.mapper.approval.ApprovalNodeMapper;
import com.newway.back.mapper.approval.ApprovalTaskMapper;
import com.newway.back.mapper.approval.ApprovalTemplateMapper;
import com.newway.back.util.SecurityUserContext;
import com.newway.exception.BusinessException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class ApprovalCommonService {

    @Resource
    ApprovalTemplateMapper approvalTemplateMapper;

    @Resource
    ApprovalNodeMapper approvalNodeMapper;

    @Resource
    ApprovalInstanceMapper approvalInstanceMapper;

    @Resource
    ApprovalTaskMapper approvalTaskMapper;

    @Resource
    SysUserMapper sysUserMapper;

    public Integer determineApprover(ApprovalNode firstNode) {
        //用户指定人
        if (firstNode.getApproverType().equals( ApproverTypeEnum.USER.getValue())) {
            SysUserEntity sysUserEntity = sysUserMapper.selectById(SecurityUserContext.getCurrentUserInfo().getId());
            return sysUserEntity.getId();
        } else {
            // 根据角色查询对应用户，这里暂时写死 TODO
            List<Integer> userIds = sysUserMapper.selectUserIdByRoles(firstNode.getApproverValue());
            if(userIds.size()==0){
                throw new BusinessException(firstNode.getNodeLevel()+"没有对应的审批人员");
            }
            return userIds.get(0);
        }
    }

    public Integer getLatestVersion(Integer templateId) {
        ApprovalTemplate approvalTemplate = approvalTemplateMapper.selectById(templateId);
        if(approvalTemplate==null){
            throw new BusinessException("当前业务类型不存在");
        }
        return approvalTemplateMapper.selectById(templateId).getVersion();
    }
    public ApprovalNode getFirstNode(Integer templateId) {
        QueryWrapper<ApprovalNode> query = new QueryWrapper<>();
        query.eq("template_id", templateId)
                .eq("version", getLatestVersion(templateId))
                .orderByAsc("node_level")
                .last("LIMIT 1");
        return approvalNodeMapper.selectOne(query);
    }

    public ApprovalNode handleNextNode(ApprovalTask approvalTask) {
        // 查找下一节点逻辑...
        Integer nodeId = approvalTask.getNodeId();
        ApprovalNode approvalNode = approvalNodeMapper.selectById(nodeId);
        QueryWrapper<ApprovalNode> approvalNodeQueryWrapper = new QueryWrapper<>();
        approvalNodeQueryWrapper.eq("template_id",approvalNode.getTemplateId());
        approvalNodeQueryWrapper.eq("version",approvalNode.getVersion());
        approvalNodeQueryWrapper.eq("node_level", approvalNode.getNodeLevel() + 1);
        ApprovalNode result = approvalNodeMapper.selectOne(approvalNodeQueryWrapper);
        return result;
    }
}
